#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "request.pb.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/net/TcpClient.h"
#include "muduo/base/CountDownLatch.h"

#include <functional>

class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<wangshu::TranslateResponse> TransResponsePtr;
    typedef std::shared_ptr<wangshu::AddResponse> AddResponsePtr;

    Client(const std::string& serverip, int serverport): _latch(1), _client(_loopthread.startLoop(), 
        muduo::net::InetAddress(serverip, serverport), "Client"), 
        _dispatcher(std::bind(&Client::OnUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<wangshu::TranslateResponse>(std::bind(&Client::OnTranslate,
            this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<wangshu::AddResponse>(std::bind(&Client::OnAdd,
            this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        // 设置消息回调处理函数
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage,
            &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        // 设置连接回调处理函数
        _client.setConnectionCallback(std::bind(&Client::Onconnection, this, std::placeholders::_1));
    }

    void Add(int num1, int num2)
    {
        wangshu::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        send(&req);
    }
    void translate(const std::string& msg)
    {
        wangshu::TranslateRequest req;
        req.set_msg(msg);
        send(&req);
    }
    void connect()
    {
        _client.connect();
        _latch.wait();
    }
private:
    void OnUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
    }
    void OnTranslate(const muduo::net::TcpConnectionPtr &conn, const TransResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "翻译结果：" << message->msg() << std::endl;
    }
    void OnAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "加法结果：" << message->result() << std::endl;
    }
    void Onconnection(const ::muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            _latch.countDown();
            _conn = conn;
        }
        else
        {
            _conn.reset();
        }
    }

    bool send(const google::protobuf::Message* msg) {
        if (_conn->connected()) {//连接状态正常，再发送，否则就返回false
            _codec.send(_conn, *msg);
            return true;
        }
        return false;
    }   
private:
    muduo::CountDownLatch _latch;
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpClient _client;
    ProtobufCodec _codec;
    ProtobufDispatcher _dispatcher;
    muduo::net::TcpConnectionPtr _conn;
};

int main()
{
    Client client("127.0.0.1", 8088);
    client.connect();

    client.Add(13, 14);
    client.translate("wangshu");

    sleep(1);
    return 0;
}